-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtests_binary_tree.py
68 lines (62 loc) · 3.01 KB
/
tests_binary_tree.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import sys
import unittest
from io import StringIO
ORIGINAL_STDOUT = sys.stdout
from binary_tree_implementation import BinaryTree
class BinaryTreeTest(unittest.TestCase):
def binary_in_order_traversal(self):
tree = BinaryTree("*",
left=BinaryTree("+",
left=BinaryTree("3",iterable_strategy="inorder"),
right=BinaryTree("2",iterable_strategy="inorder"),
iterable_strategy="inorder"),
right=BinaryTree("-",
left=BinaryTree("9", iterable_strategy="inorder"),
right=BinaryTree("6", iterable_strategy="inorder"),
iterable_strategy="inorder"),
iterable_strategy="inorder")
nodes = [node for node in tree]
expected_nodes = ["3", "+", "2", "*", "9", "-", "6"]
self.assertEqual(expected_nodes, nodes)
def binary_post_order_traversal(self):
tree = BinaryTree("*",
left=BinaryTree("+",
left=BinaryTree("3", iterable_strategy="postorder"),
right=BinaryTree("2", iterable_strategy="postorder"),
iterable_strategy="postorder"),
right=BinaryTree("-",
left=BinaryTree("9", iterable_strategy="postorder"),
right=BinaryTree("6", iterable_strategy="postorder"),
iterable_strategy="postorder"),
iterable_strategy="postorder")
nodes = [node for node in tree]
expected_nodes = ["3", "2", "+", "9", "6", "-", "*"]
self.assertEqual(expected_nodes, nodes)
def print_indented_pre_order(self):
output = StringIO()
sys.stdout = output
tree = BinaryTree("*",
left=BinaryTree("+",
left=BinaryTree("3", iterable_strategy="postorder"),
right=BinaryTree("2", iterable_strategy="postorder"),
iterable_strategy="postorder"),
right=BinaryTree("-",
left=BinaryTree("9", iterable_strategy="postorder"),
right=BinaryTree("6", iterable_strategy="postorder"),
iterable_strategy="postorder"),
iterable_strategy="postorder")
try:
tree.print()
result = output.getvalue()
expected_output = """*
+
3
2
-
9
6"""
self.assertEqual(result, expected_output)
finally:
sys.stdout = ORIGINAL_STDOUT
if __name__ == '__main__':
unittest.main()